package com.ipzoe.web.controller.cms;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ipzoe.common.utils.SecurityUtils;
import com.ipzoe.common.utils.StringUtils;
import com.ipzoe.web.model.cms.FeedbackQueryDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ipzoe.common.annotation.Log;
import com.ipzoe.common.core.controller.BaseController;
import com.ipzoe.common.core.domain.AjaxResult;
import com.ipzoe.common.enums.BusinessType;
import com.ipzoe.cms.domain.Feedback;
import com.ipzoe.cms.service.IFeedbackService;
import com.ipzoe.common.utils.poi.ExcelUtil;
import com.ipzoe.common.core.page.TableDataInfo;

/**
 * 反馈信息Controller
 * 
 * @author eric
 * @date 2020-08-17
 */
@RestController
@RequestMapping("/cms/feedback" )
public class FeedbackController extends BaseController {

    @Autowired
    private IFeedbackService iFeedbackService;

    /**
     * 查询反馈信息列表
     */
    @PreAuthorize("@ss.hasPermi('cms:feedback:list')")
    @GetMapping("/list")
    public TableDataInfo list(FeedbackQueryDTO feedback)
    {
        startPage();
        LambdaQueryWrapper<Feedback> lqw = new LambdaQueryWrapper<Feedback>();
        if (feedback.getMemberId() != null){
            lqw.eq(Feedback::getMemberId ,feedback.getMemberId());
        }
        if (StringUtils.isNotBlank(feedback.getNickname())){
            lqw.like(Feedback::getNickname ,feedback.getNickname());
        }
        if (StringUtils.isNotBlank(feedback.getMobile())){
            lqw.eq(Feedback::getMobile ,feedback.getMobile());
        }
        if (StringUtils.isNotBlank(feedback.getContent())){
            lqw.eq(Feedback::getContent ,feedback.getContent());
        }
        if (feedback.getStatus() != null){
            lqw.eq(Feedback::getStatus ,feedback.getStatus());
        }
        lqw.apply(StrUtil.isNotEmpty(feedback.getSearchBeginTime()),"date_format(create_time,'%y%m%d') >= date_format({0},'%y%m%d')",feedback.getSearchBeginTime());
        lqw.apply(StrUtil.isNotEmpty(feedback.getSearchEndTime()),"date_format(create_time,'%y%m%d') <= date_format({0},'%y%m%d')",feedback.getSearchEndTime());
        List<Feedback> list = iFeedbackService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出反馈信息列表
     */
    @PreAuthorize("@ss.hasPermi('cms:feedback:export')" )
    @Log(title = "反馈信息" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(Feedback feedback) {
        LambdaQueryWrapper<Feedback> lqw = new LambdaQueryWrapper<Feedback>(feedback);
        List<Feedback> list = iFeedbackService.list(lqw);
        ExcelUtil<Feedback> util = new ExcelUtil<Feedback>(Feedback. class);
        return util.exportExcel(list, "feedback" );
    }

    /**
     * 获取反馈信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('cms:feedback:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iFeedbackService.getById(id));
    }


    @PreAuthorize("@ss.hasPermi('cms:feedback:hand')" )
    @PutMapping(value = "/hand" )
    public AjaxResult hand(@RequestBody Feedback feedback) {

        feedback.setStatus(1).setUpdateBy(SecurityUtils.getUsername());
        return AjaxResult.success(iFeedbackService.updateById(feedback));
    }

    /**
     * 新增反馈信息
     */
    @PreAuthorize("@ss.hasPermi('cms:feedback:add')" )
    @Log(title = "反馈信息" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Feedback feedback) {
        return toAjax(iFeedbackService.save(feedback) ? 1 : 0);
    }

    /**
     * 修改反馈信息
     */
    @PreAuthorize("@ss.hasPermi('cms:feedback:edit')" )
    @Log(title = "反馈信息" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Feedback feedback) {
        return toAjax(iFeedbackService.updateById(feedback) ? 1 : 0);
    }

    /**
     * 删除反馈信息
     */
    @PreAuthorize("@ss.hasPermi('cms:feedback:remove')" )
    @Log(title = "反馈信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iFeedbackService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
